package com.cwl.dao.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.cwl.dao.QuestionDao;
import com.cwl.domain.Questions;


public class QuestionDaoImpl extends HibernateDaoSupport implements QuestionDao {
	public Questions findById(Integer qid){
		String hql = "from Questions where ques_id=?";
		List<Questions> questionslist=this.getHibernateTemplate().find(hql,qid);
		if(questionslist.size()>0){
			Questions questions = questionslist.get(0);
			return questions;
		}else{
			return null;
		}
		
		
	}

	public ArrayList<Questions> findAll(int[] array) {
		StringBuffer sb = new StringBuffer();
		StringBuffer sb2 = new StringBuffer();
		for(int i=0;i<array.length-1;i++){
			sb.append("?,");
		}
		sb.append("?");
		for(int i=0;i<array.length-1;i++){
			sb2.append(array[i]+",");
		}
		sb2.append(array[array.length-1]);
		// ORDER BY FIND_IN_SET(id,'343,123,32,10,6,981,651,98,129');
		String hql ="from Questions where ques_id in("+sb2.toString()+") order by FIND_IN_SET(ques_id,'"+sb2.toString()+"')";		
		int[] array2 = new int[array.length*2];
		for(int i=0;i<array.length;i++){
			array2[i] = array[i];
		}
		for(int i=array.length;i<array.length*2;i++){
			array2[i] = array[i-array.length];
		}
		for(int i=0;i<array2.length;i++){
			System.out.println(array2[i]);
		}
		ArrayList<Questions> questionslist = (ArrayList<Questions>) this.getHibernateTemplate().find(hql);
		return questionslist;
	}

	public int getCount() {
		String hql = "from Questions";
		ArrayList<Questions> questionslist = (ArrayList<Questions>)this.getHibernateTemplate().find(hql);
		return questionslist.size();
	}

	public void save(Questions questions) {
		this.getHibernateTemplate().save(questions);
		
	}
}
